#include "poll.h"

#ifdef POLL_DEBUGB
#define dbg_print(fmt, ...)			\
	do{								\
		printk("POLL: ");			\
		printk(fmt, ##__VA_ARGS__);	\
		printk("\r\n");				\
	}while(0)
#else
#define dbg_print(fmt, ...)
#endif

DEFINE_KFIFO(gfifo, char, 32);
DECLARE_WAIT_QUEUE_HEAD(poll_rq);
DECLARE_WAIT_QUEUE_HEAD(poll_wq);

//打开设备
static int poll_open(struct inode *inode, struct file *file)
{
	return 0;
}

//释放设备
static int poll_release(struct inode *inode, struct file *file)
{
	return 0;
}

//读设备
static ssize_t poll_read(struct file *file, char __user *buf, size_t len, loff_t *pos)
{
	unsigned int copied;

	if(kfifo_is_empty(&gfifo))
	{
		dbg_print("fifo is empty\r\n");

		//以非阻塞式打开
		if(file->f_flags & O_NONBLOCK)
			return -EAGAIN;

		//挂起当前进程到等待队列，能被信号唤醒
		if(wait_event_interruptible_exclusive(poll_rq, !kfifo_is_empty(&gfifo)))
			return -ERESTARTSYS;
	}

	//将FIFO中的数据读取到应用层
	if(kfifo_to_user(&gfifo, buf, len, &copied) != 0)
	{
		dbg_print("Bad address\r\n");
		return -EFAULT;
	}

	//如果可以写入则唤醒写进程
	if(!kfifo_is_full(&gfifo))
		wake_up_interruptible(&poll_wq);

	return copied;
}

//写设备
static ssize_t poll_write(struct file *file, const char __user *buf, size_t len, loff_t *pos)
{
	unsigned int copied;

	if(kfifo_is_full(&gfifo))
	{
		dbg_print("fifo is full\r\n");
		//以非阻塞式打开
		if(file->f_flags & O_NONBLOCK)
			return -EAGAIN;

		//挂起当前进程到等待队列，能被信号唤醒
		if(wait_event_interruptible_exclusive(poll_wq, !kfifo_is_full(&gfifo)))
			return -ERESTARTSYS;
	}

	//将应用层的数据写入FIFO
	if(kfifo_from_user(&gfifo, buf, len, &copied) != 0)
	{
		dbg_print("Bad address\r\n");
		return -EFAULT;
	}

	//如果有数据可读则唤醒读进程
	if(!kfifo_is_empty(&gfifo))
		wake_up_interruptible(&poll_rq);

	return copied;
}

static unsigned int poll_poll(struct file *filp, struct poll_table_struct *p)
{
	unsigned int mask = 0;
	
	//调用poll_wait，将进程加入到可能会改变进程状态的等待队列中，并将等待队列的信息添加到以poll_table扩展的
	//数据结构中，此函数不会引起进程休眠
	poll_wait(filp, &poll_rq, p);
	poll_wait(filp, &poll_wq, p);
	
	//如果发生读事件
	//POLLIN 通常等价 POLLRDNORM ，表示发生读事件，即驱动有数据可读
	if(!kfifo_is_empty(&gfifo))
	{
		dbg_print("read");
		mask |= POLLIN | POLLRDNORM;
	}
	
	//如果发生写事件
	//POLLOUT 通常等价 POLLWRNORM ，表示发生写事件，即驱动可以接收应用层数据
	if(!kfifo_is_full(&gfifo))
	{
		dbg_print("write");
		mask |= POLLOUT | POLLWRNORM;
	}

	//返回相应的事件，没有发生任何事件则返回0
	return mask;
}

static struct file_operations ops = {
	.owner = THIS_MODULE,
	.write = poll_write,
	.read = poll_read,
	.poll = poll_poll,
	.open = poll_open,
	.release = poll_release,
};

static struct miscdevice poll_misc = {
	.name = POLL_NAME,
	.minor = POLL_MINOR,
	.fops = &ops,
};

static int __init poll_init(void)
{
	int err = 0;

	printk("poll fifo init\r\n");
	//注册混杂设备
	err = misc_register(&poll_misc);
	if(err != 0)
	{
		dbg_print("register misc failed\r\n");
		return err;
	}
	return 0;
}

static void __exit poll_exit(void)
{
	printk("poll fifo exit\r\n");
	//注销混杂设备
	misc_deregister(&poll_misc);
}

module_init(poll_init);
module_exit(poll_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("lf");
MODULE_DESCRIPTION("poll");
